package com.cloudcross.ssp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.model.AccountResources;
import com.cloudcross.ssp.model.Resources;
import com.cloudcross.ssp.model.ResourcesRole;
import com.cloudcross.ssp.service.IResourcesService;
import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.pulgin.mybatis.plugin.PageView;
@Service
public class ResourcesService implements IResourcesService {
	@Autowired
	private GenericIBatisDao myBatisDao;
	@Override
	public PageView query(PageView pageView, Resources t) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", pageView);
		map.put("t", t);
		List<Resources> list = myBatisDao.getList("resourcesSqlMapper.query", map);
		pageView.setRecords(list);
		return pageView;
	}

	@Override
	public List<Resources> queryAll(Resources t) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.queryAll",t);
	}
	public boolean deleteResourceByAccount(Long accountId){
		myBatisDao.save("resourcesSqlMapper.deleteAccountResources", accountId);
		return true;
	}
	@Override
	public void delete(String id) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("resourcesSqlMapper.delete", id);
	}
	
	@Override
	public void update(Resources t) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("resourcesSqlMapper.update", t);
	}

	@Override
	public Resources getById(String id) {
		// TODO Auto-generated method stub
		return myBatisDao.get("resourcesSqlMapper.getById", id);
	}

	@Override
	public void add(Resources t) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("resourcesSqlMapper.add",t);
	}
//-----------------------↓↓↓↓↓↓↓↓↓↓↓↓↓↓这部分主要用于对于t_res_accounts表的增、查、改、删-----------------------------------------------------------------------------------------
	@Override
	public List<Resources> findResourcesByAccount(Long id) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.findResourcesByAccount",id);
	}
		@Override
	public List<Resources> findAccountResourcess(String id) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.findAccountResourcess",id);
	}
//-----------------------↑↑↑↑↑↑↑↑↑↑↑↑↑这部分主要用于对于t_res_accounts表的增、查、改、删-----------------------------------------------------------------------------------------
	@Override
	public List<Resources> findRoleRes(String roleId) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.findRoleRes", roleId);
	}

	@Override
	public List<Resources> queryByParentId(Resources resources) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.queryByParentId", resources);
	}

	@Override
	public void updateSortOrder(List<Resources> menus) {
		// TODO Auto-generated method stub
		for (Resources m : menus) {
			myBatisDao.save("resourcesSqlMapper.updateSortOrder", m);
		}
	}

	@Override
	public void addRoleRes(String roleId, List<String> list) {
		// TODO Auto-generated method stub
		myBatisDao.save("resourcesSqlMapper.deleteResourcesRole", roleId);
		for (String string : list) {
			ResourcesRole rr = new ResourcesRole();
			rr.setRoleId(roleId);
			rr.setResId(string);
			myBatisDao.save("resourcesSqlMapper.addRoleRes", rr);
        }

	}

	@Override
	public Resources isExist(String menuName) {
		// TODO Auto-generated method stub
		return myBatisDao.get("resourcesSqlMapper.isExist", menuName);
	}

	@Override
	public int getMaxLevel() {
		// TODO Auto-generated method stub
		return myBatisDao.get("resourcesSqlMapper.getMaxLevel",null);
	}

	@Override
	public int countByParams(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Resources> findByParams(Map<String, Object> paramMap,
			Pager pager) {
		// TODO Auto-generated method stub
		return null;
	}

	
	
	//添加权限（先删除已有权限，在添加新的权限）
	public boolean addAccountResourcess(Long accountId, List<Long> list) {
		//先将权限表中所有对应这一accountId的权限都删掉
		myBatisDao.save("resourcesSqlMapper.deleteAccountResources", accountId);
		for (Long l : list) {
			//再循环将新传进来的权限一个一个添加进来
			AccountResources ar = new AccountResources();
			ar.setAccountId(accountId);
			ar.setResourceId(l);
			myBatisDao.save("resourcesSqlMapper.addAccountResourcess", ar);
        }
		return true;
	}
	//根据账户Id查询权限
	public List<AccountResources> queryResourcesByAccountId(Long accountId){
		return myBatisDao.getList("resourcesSqlMapper.queryResourcesById",accountId);
	}
	
	//根据accountId删除权限
	public boolean deleteAccountResourcess(Long accountId){
		myBatisDao.save("resourcesSqlMapper.deleteAccountResources", accountId);
		return true;
	}

	@Override
	public boolean deleteAccountNotinResources(Long accountId) {
		// TODO 删掉t_res_notin_accounts表中对应accountId
		myBatisDao.save("resourcesSqlMapper.deleteAccountNotinResources", accountId);
		return true;
	}

	@Override
	public boolean addAccountNotinResourcess(Long accountId, List<Long> list) {
		// TODO Auto-generated method stub
		//先将权限表中所有对应这一accountId的权限都删掉
		myBatisDao.save("resourcesSqlMapper.deleteAccountNotinResources", accountId);
		for (Long l : list) {
			//再循环将新传进来的权限一个一个添加进来
			AccountResources ar = new AccountResources();
			ar.setAccountId(accountId);
			ar.setResourceId(l);
			myBatisDao.save("resourcesSqlMapper.addAccountNotinResourcess", ar);
        }
		return true;
	}

	@Override
	public List<AccountResources> queryNotinResourcesById(Long accountId) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.queryNotinResourcesById",accountId);
	}

	@Override
	public List<Resources> findAllResourcesByAccountId(Long id) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.findAllResourcesByAccountId",id);
	}

	@Override
	public List<Resources> findAllResources(Map<String, Object> condition,Pager pager) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.findAllResources",condition);
	}

	@Override

	public List<Resources> findAllResources() {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.findAllResourcesNoCondition");
	}

	@Override
	public List<Resources> findResourcesById(long id) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.findResourcesById",id);
	}

	public boolean deleteResource(Integer id) {
		// TODO Auto-generated method stub
		myBatisDao.delete("resourcesSqlMapper.delete", id);
		return true;
	}

	@Override
	public List<Resources> findAllRootResources() {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.queryRootResources");
	}

	@Override
	public List<Resources> findAllResourceById(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.queryResourcesByIdAndType", paramMap);
	}
}
